var pathname = common.locationParams()["pathname"] || common.ctx + '/html/fairList.html';
$("#yzm").click(function () {
    this.src = common.sctx + '/verifyCode/generate.do?type=LOGIN&key=' + Math.random();
});
$("#yzm").trigger("click");

function cleanForm() {
    //$("#phone").focus();
    //$("#phone").val("");
    $("#Vcode").val("");
    $("#icode").val("");
    $("#pwd").val("")
}

var loginType = 1;
$("#sms-login-tab").click(function () {
    $("#sms-login-tab").addClass("active");
    $(".sms-input").show();

    $("#pwd-login-tab").removeClass("active");
    $(".pwd-input").hide();

    cleanForm();
    loginType = 1;
});
$("#pwd-login-tab").click(function () {
    $("#pwd-login-tab").addClass("active");
    $(".pwd-input").show();

    $("#sms-login-tab").removeClass("active");
    $(".sms-input").hide();

    cleanForm();
    loginType = 2;
});

var wait = 60;
var click_able = true;

function time(o) {
    if (wait === 0) {
        if (o.is('span')) {
            o.text("获取");
        } else {
            o.val("获取");
        }
        click_able = true;
    } else {
        if (o.is('span')) {
            o.text("重新发送(" + wait + ")");
        } else {
            o.val("重新发送(" + wait + ")");
        }
        wait--;
        setTimeout(function () {
            time(o)
        }, 1000)
    }
};

function sendVcode(phone, icode) {
    var sendResult = false;
    http.get({
        url: common.sctx + '/commons/sendSmsVerifyCode.do?mobile=' + phone + '&imgVerifyCode=' + icode,
        async: false,
        success: function () {
            sendResult = true;
        },
        error: function (res) {
            alert(res.message);
            $("#yzm").trigger("click");
            $("#icode").focus();
        }
    });
    return sendResult;
};

function getVcode(phone, icode, span_vcode) {
    if (click_able) {

        if (validatePhoneNumber($(phone).val())) {
            phone = $(phone).val();
        } else {
            alert('手机号码格式有误！');
            $(phone).focus();
            return;
        }
        if ($(icode).val() === "") {
            alert('请输入图形码');
            $(icode).focus();
            return;
        }

        click_able = !sendVcode(phone, $(icode).val());

        if (!click_able) {
            wait = 60;
            time($(span_vcode));
        } else {
            $(icode).focus();
        }

    }
};

$("#signout").click(function () {
    logout(common.ctx + "/html/login.html");
});


/** 登陆*/
$("#login-button").click(function () {
    var mobile;
    var password;
    var verifyCode;
    if ($(".div_phone").is(":visible")) {
        if (!validatePhoneNumber($("#phone").val())) {
            $(".overlay").hide();
            $(".delay").hide();
            alert('手机号码格式有误！');
            $("#phone").focus();
            return false;
        }
        mobile = $("#phone").val();
    } else {
        mobile = $(".p_phone").text();
    }

    if (loginType === 1) {
        if (!controlValueIsEmpty("vcode", "验证码")) {
            return false;
        }
        verifyCode = $("#vcode").val();
    } else if (loginType === 2) {
        if (!controlValueIsEmpty("icode", "验证码")) {
            return false;
        }
        verifyCode = $("#icode").val();
        password = $("#pwd").val();
        if (!password) {
            alert('密码不能为空！');
            $("#pwd").focus();
            return false;
        }
    }

    $(".overlay").css({"display": "block"});
    $(".delay").css({"display": "block"});


    http.post({
        url: common.sctx + "/j_spring_security_check?rememberMe=true",
        data: {
            "code": mobile,
            "verifyCode": verifyCode,
            "loginType": loginType,
            "password": password,
            "rememberMe": true
        },
        success: function (res) {
            window.location.href = decodeURIComponent(pathname);
        },
        error: function (res) {
            $(".overlay").hide();
            $(".delay").hide();
            alert(res.message);
        }
    });
});

$(".sms-code-button").click(function () {
    getVcode($("#phone"), $("#icode"), $(this));
});